package com.legendshop.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 记录开始和结束时间
 */
public class LogDateUtil {

	//密码每过12个月需要重修修改
	public static Integer PASSWORD_CHANGE_PERIOD = 12;
	/**
	 * 计算时间
	 * 
	 * @param myDate
	 * @return
	 */
	public static Date parseOffsetDate(Date myDate, int offset) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(myDate);
		cal.add(Calendar.DATE, offset);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String s = sdf.format(cal.getTime());
		Date date = null;
		try {
			date = sdf.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 得到本周周一
	 * 
	 * @return yyyy-MM-dd
	 */
	public static Date getMondayOfThisWeek() {
		Calendar c = Calendar.getInstance();
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0)
			day_of_week = 7;
		c.add(Calendar.DATE, -day_of_week + 1);
		return c.getTime();
	}

	/**
	 * 得到本周周日
	 * 
	 * @return yyyy-MM-dd
	 */
	public static Date getSundayOfThisWeek() {
		Calendar c = Calendar.getInstance();
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0)
			day_of_week = 7;
		c.add(Calendar.DATE, -day_of_week + 7);
		return c.getTime();
	}

	public static Date getMondayOfLastWeek() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getMondayOfThisWeek());
		cal.add(Calendar.DATE, -7);
		return cal.getTime();
	}

	public static Date getSundayOfLastWeek() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getSundayOfThisWeek());
		cal.add(Calendar.DATE, -7);
		return cal.getTime();
	}

	public static Date getFirstDayOfLastMonth() {
		// 获取前月的第一天
		Calendar cal = Calendar.getInstance();// 获取当前日期
		cal.add(Calendar.MONTH, -1);
		cal.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		return cal.getTime();
	}

	public static Date getLastDayOfLastMonth() {
		// 获取前月的最后一天
		Calendar cale = Calendar.getInstance();
		cale.set(Calendar.DAY_OF_MONTH, 0);// 设置为1号,当前日期既为本月第一天
		return cale.getTime();
	}

	public static Date getFirstDayOfThisMonth() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		return c.getTime();
	}

	public static Date getLastDayOfThisMonth() {
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		return ca.getTime();
	}

	/**
	 * 12个月之后，密码需要修改
	 * @return
	 */
	public static Date getPasswordChangeDate() {
		Date myDate = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(myDate);
		cal.add(Calendar.MONTH, PASSWORD_CHANGE_PERIOD);
		return cal.getTime();
	}

	public static void main(String[] args) {
		System.out.println(getFirstDayOfThisMonth());
		System.out.println(getLastDayOfThisMonth());

		System.out.println(getMondayOfLastWeek());

		System.out.println(getSundayOfLastWeek());
	}
}
